home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15288 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  77 lines

  1. Newsgroups: comp.lang.c++
  2. Path: artemis.sto.fdata.se!news
  3. From: Niklas Mellin <niklas.mellin@sto.fdata.se>
  4. Subject: Re: Function Prototypes in C++
  5. Sender: news@artemis.sto.fdata.se (UseNet NetNews)
  6. Message-ID: <3163C668.2635@sto.fdata.se>
  7. Date: Thu, 4 Apr 1996 12:54:00 GMT
  8. Content-Transfer-Encoding: 7bit
  9. Content-Type: text/plain; charset=us-ascii
  10. References: <4junnq$2k1@phoenix.csc.calpoly.edu>
  11. Mime-Version: 1.0
  12. X-Mailer: Mozilla 2.0 (WinNT; I)
  13. Organization: WM-data F÷rsvarsdata AB, Sweden
  14.  
  15. Nicholas R Bonfilio wrote:
  16. > I know that function prototypes are required in standard C++ as opposed to C
  17. > where they are not required.  But, what I don't know for certain is the rules
  18. > regarding the scoping of function protos...
  19.  
  20. Actually function prototypes are not required if the definition of the function
  21. is seen before the first call to that function. The main-function is one example
  22. of a function that is normally not prototyped.
  23.  
  24. > That is, can I declare the prototypes as "global": external to the main()
  25. > routine?
  26.  
  27. Yes.
  28.  
  29. > Should all prototypes go inside of the main() routine?
  30.  
  31. Normally no.
  32.  
  33. > Or should the prototypes be placed inside of each routine where the
  34. > particular function is called?  (This could have been asked in the C lang
  35. > discussion group, I realize.)
  36.  
  37. If you prototype a function inside another function the prototype is only
  38. visible within that functions scope.
  39.  
  40. Normally prototypes for functions that are called from more than one 
  41. translation unit are put in header files that can be included to save
  42. typing and typeing errors.
  43.  
  44. [Example substituted with another one]
  45.  
  46. #include <math.h> // global function prototypes for sin, cos etc.
  47.  
  48. void f(); // global prototype
  49.  
  50. int main()
  51. {
  52.   void g(); // local prototype
  53.   g();      // ok, prototyped above
  54.   h();      // error, no prototype for h 
  55.   sin(0.2); // ok, global prototyp included in math.h
  56.   return 0;
  57. }
  58.  
  59. void h()
  60. {
  61.   f(); // ok, f prototyped globally above
  62.   g(); // error, prototype in main not visible here
  63. }
  64.  
  65. void hh()
  66. {
  67.   h(); // ok, definition of h seen above
  68. }
  69.  
  70. void g()
  71. {
  72. }
  73.  
  74. ---
  75. Niklas Mellin
  76.